package com.menggudf.service.impl;

import com.menggudf.common.utils.DateUtils;
import com.menggudf.dao.MedicalRecordDao;
import com.menggudf.entity.MedicalRecord;
import com.menggudf.entity.MyCaseHistory;
import com.menggudf.model.MedicalRecordModel;
import com.menggudf.model.MyCaseHistoryModel;
import com.menggudf.model.ResultModel;
import com.menggudf.service.MedicalRecordService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service
public class MedicalRecordServiceImpl implements MedicalRecordService {

    @Autowired
    private MedicalRecordDao medicalRecordDao;

    private static final Logger log = LoggerFactory
            .getLogger(MedicalRecordService.class);

    @Override
    @Transactional(value = "medicalTM", rollbackFor=Exception.class)
    public MedicalRecordModel getById(Integer id) {
        MedicalRecord m = medicalRecordDao.findById(id);
        MedicalRecordModel mm = new MedicalRecordModel();
        BeanUtils.copyProperties(m,mm);
        return mm;
    }

    @Override
    @Transactional(value = "medicalTM", readOnly=true)
    public Map<String, Object> getMedicalRecords(
            String userCode,
            int pageSize,
            int offset) {
        Map<String, Object> map = new HashMap<String, Object>();
        List<MedicalRecord> list = medicalRecordDao.getMedicalRecords(userCode,pageSize,offset);
        long count = medicalRecordDao.getMedicalRecordsCount(userCode);
        List<MedicalRecordModel> mlist = new ArrayList<MedicalRecordModel>();
        MedicalRecordModel m = null;
        for(MedicalRecord my : list){
            m = new MedicalRecordModel();
            BeanUtils.copyProperties(my,m);
            mlist.add(m);
        }
        map.put("total", count);
        map.put("rows", mlist);
        return map;
    }

    @Override
    @Transactional(value = "medicalTM", rollbackFor=Exception.class)
    public ResultModel createMedicalRecord(MedicalRecordModel model) {
        try {
            MedicalRecord my = new MedicalRecord();
            my.setCode(UUID.randomUUID().toString());
            Date time = new Date();
            my.setCreatedTime(time);
            my.setUsercode(model.getUserCode());
            my.setUsername(model.getUsername());
            my.setZhushu(model.getZhushu());
            my.setAskrecord(model.getAskrecord());
            my.setMedicalrecord(model.getMedicalrecord());
            my.setName(model.getZhushu() + "-"+ DateUtils.dateFormat1(time));
            my.setStatus(1);//可用，0为删除
            medicalRecordDao.persist(my);
            return ResultModel.success("创建成功", my);
        } catch (Exception e) {
            log.error("createMedicalRecord fail",e);
        }
        return ResultModel.fail("创建失败", null);
    }

    @Override
    @Transactional(value = "medicalTM", readOnly=true)
    public Map<String, Object> getMedicalRecordList(String userName, int pageSize, int offset) {
        Map<String, Object> map = new HashMap<String, Object>();
        List<MedicalRecord> list = medicalRecordDao.getMedicalRecordList(userName,pageSize,offset);
        long count = medicalRecordDao.getMedicalRecordListCount(userName);
        List<MedicalRecordModel> mlist = new ArrayList<MedicalRecordModel>();
        MedicalRecordModel m = null;
        for(MedicalRecord my : list){
            m = new MedicalRecordModel();
            BeanUtils.copyProperties(my,m);
            m.setUsername(my.getUsername());
            m.setUserCode(my.getUsercode());
            mlist.add(m);
        }
        map.put("total", count);
        map.put("rows", mlist);
        return map;
    }

    @Override
    @Transactional(value = "medicalTM", rollbackFor=Exception.class)
    public ResultModel editMedicalRecord(MedicalRecordModel model) {
        if(StringUtils.isBlank(model.getCode())){
            return ResultModel.fail("更新失败", null);
        }
        MedicalRecord my = medicalRecordDao.findByCode(model.getCode());
        if(my==null){
            return ResultModel.fail("更新失败", null);
        }
        try {
            if(!StringUtils.isBlank(model.getMedicalrecord())){
                my.setMedicalrecord(model.getMedicalrecord());
            }
            if(!StringUtils.isBlank(model.getPingjia())){
                my.setPingjia(model.getPingjia());
            }
            if(!StringUtils.isBlank(model.getJiedu())){
                my.setJiedu(model.getJiedu());
            }
            medicalRecordDao.merge(my);
            return ResultModel.success("更新成功", null);
        } catch (Exception e) {
            log.error("editMedicalRecord fail",e);
        }
        return ResultModel.fail("更新失败", null);
    }

    @Override
    @Transactional(value = "medicalTM", rollbackFor=Exception.class)
    public ResultModel delById(Integer id) {
        MedicalRecord my = medicalRecordDao.findById(id);
        if(my==null){
            return ResultModel.fail("删除失败", null);
        }
        try {
            my.setStatus(0);
            medicalRecordDao.merge(my);
            return ResultModel.success("删除成功", null);
        } catch (Exception e) {
            log.error("delById fail",e);
        }
        return ResultModel.fail("删除失败", null);
    }
}
